package com.macro.mall.controller.pms;

import com.github.pagehelper.Page;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.common.api.input.PageConstants;
import com.macro.mall.dto.pms.InsertProductReqDto;
import com.macro.mall.dto.pms.PagedQueryProductReqDto;
import com.macro.mall.dto.pms.UpdateProductReqDto;
import com.macro.mall.pms.entity.ProductEntity;
import com.macro.mall.pms.service.ProductService;
import com.macro.mall.pms.service.bo.InsertProductInput;
import com.macro.mall.pms.service.bo.PagedQueryProductInput;
import com.macro.mall.pms.service.bo.UpdateProductInput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品信息控制器
 *
 * @author xuyanjun
 * @date 2019-12-22
 */
@Api(tags = "商品信息")
@Slf4j(topic = "ProductController")
@RestController
@RequestMapping("/product/v2")
public class ProductControllerV2 {

    @Autowired
    private ProductService productService;

    /**
     * 新增商品信息
     *
     * @param reqDto reqDto
     * @return 商品信息
     */
    @ApiOperation("新增商品信息")
    @PostMapping("insert")
    public CommonResult<ProductEntity> insert(@RequestBody InsertProductReqDto reqDto) {
        InsertProductInput insertProductInput = new InsertProductInput();
        insertProductInput
                .setBrandId(reqDto.getBrandId())
                .setProductCategoryId(reqDto.getProductCategoryId())
                .setFeightTemplateId(reqDto.getFeightTemplateId())
                .setProductAttributeCategoryId(reqDto.getProductAttributeCategoryId())
                .setName(reqDto.getName())
                .setPic(reqDto.getPic())
                .setProductSn(reqDto.getProductSn())
                .setDeleteStatus(reqDto.getDeleteStatus())
                .setPublishStatus(reqDto.getPublishStatus())
                .setNewStatus(reqDto.getNewStatus())
                .setRecommandStatus(reqDto.getRecommandStatus())
                .setVerifyStatus(reqDto.getVerifyStatus())
                .setSort(reqDto.getSort())
                .setSale(reqDto.getSale())
                .setPrice(reqDto.getPrice())
                .setPromotionPrice(reqDto.getPromotionPrice())
                .setGiftGrowth(reqDto.getGiftGrowth())
                .setGiftPoint(reqDto.getGiftPoint())
                .setUsePointLimit(reqDto.getUsePointLimit())
                .setSubTitle(reqDto.getSubTitle())
                .setDescription(reqDto.getDescription())
                .setOriginalPrice(reqDto.getOriginalPrice())
                .setStock(reqDto.getStock())
                .setLowStock(reqDto.getLowStock())
                .setUnit(reqDto.getUnit())
                .setWeight(reqDto.getWeight())
                .setPreviewStatus(reqDto.getPreviewStatus())
                .setServiceIds(reqDto.getServiceIds())
                .setKeywords(reqDto.getKeywords())
                .setNote(reqDto.getNote())
                .setAlbumPics(reqDto.getAlbumPics())
                .setDetailTitle(reqDto.getDetailTitle())
                .setDetailDesc(reqDto.getDetailDesc())
                .setDetailHtml(reqDto.getDetailHtml())
                .setDetailMobileHtml(reqDto.getDetailMobileHtml())
                .setPromotionStartTime(reqDto.getPromotionStartTime())
                .setPromotionEndTime(reqDto.getPromotionEndTime())
                .setPromotionPerLimit(reqDto.getPromotionPerLimit())
                .setPromotionType(reqDto.getPromotionType())
                .setBrandName(reqDto.getBrandName())
                .setProductCategoryName(reqDto.getProductCategoryName());

        long id = productService.insert(insertProductInput);

        ProductEntity productEntity = productService.getById(id);

        return CommonResult.success(productEntity);
    }

    /**
     * 修改商品信息
     *
     * @param reqDto reqDto
     * @return 商品信息
     */
    @ApiOperation("修改商品信息")
    @PostMapping("update")
    public CommonResult<ProductEntity> update(@RequestBody UpdateProductReqDto reqDto) {
        UpdateProductInput updateProductInput = new UpdateProductInput();
        updateProductInput
                .setId(reqDto.getId())
                .setBrandId(reqDto.getBrandId())
                .setProductCategoryId(reqDto.getProductCategoryId())
                .setFeightTemplateId(reqDto.getFeightTemplateId())
                .setProductAttributeCategoryId(reqDto.getProductAttributeCategoryId())
                .setName(reqDto.getName())
                .setPic(reqDto.getPic())
                .setProductSn(reqDto.getProductSn())
                .setDeleteStatus(reqDto.getDeleteStatus())
                .setPublishStatus(reqDto.getPublishStatus())
                .setNewStatus(reqDto.getNewStatus())
                .setRecommandStatus(reqDto.getRecommandStatus())
                .setVerifyStatus(reqDto.getVerifyStatus())
                .setSort(reqDto.getSort())
                .setSale(reqDto.getSale())
                .setPrice(reqDto.getPrice())
                .setPromotionPrice(reqDto.getPromotionPrice())
                .setGiftGrowth(reqDto.getGiftGrowth())
                .setGiftPoint(reqDto.getGiftPoint())
                .setUsePointLimit(reqDto.getUsePointLimit())
                .setSubTitle(reqDto.getSubTitle())
                .setDescription(reqDto.getDescription())
                .setOriginalPrice(reqDto.getOriginalPrice())
                .setStock(reqDto.getStock())
                .setLowStock(reqDto.getLowStock())
                .setUnit(reqDto.getUnit())
                .setWeight(reqDto.getWeight())
                .setPreviewStatus(reqDto.getPreviewStatus())
                .setServiceIds(reqDto.getServiceIds())
                .setKeywords(reqDto.getKeywords())
                .setNote(reqDto.getNote())
                .setAlbumPics(reqDto.getAlbumPics())
                .setDetailTitle(reqDto.getDetailTitle())
                .setDetailDesc(reqDto.getDetailDesc())
                .setDetailHtml(reqDto.getDetailHtml())
                .setDetailMobileHtml(reqDto.getDetailMobileHtml())
                .setPromotionStartTime(reqDto.getPromotionStartTime())
                .setPromotionEndTime(reqDto.getPromotionEndTime())
                .setPromotionPerLimit(reqDto.getPromotionPerLimit())
                .setPromotionType(reqDto.getPromotionType())
                .setBrandName(reqDto.getBrandName())
                .setProductCategoryName(reqDto.getProductCategoryName());
        productService.update(updateProductInput);

        ProductEntity product = productService.getById(reqDto.getId());

        return CommonResult.success(product);
    }

    /**
     * 修改商品信息
     *
     * @param reqDto reqDto
     * @return 商品信息
     */
    @ApiOperation("修改商品信息")
    @PostMapping("updateCommission")
    public CommonResult<ProductEntity> updateCommission(@RequestBody UpdateProductReqDto reqDto) {
        UpdateProductInput updateProductInput = new UpdateProductInput();
        updateProductInput
                .setId(reqDto.getId())
                .setBrandId(reqDto.getBrandId())
                .setProductCategoryId(reqDto.getProductCategoryId())
                .setFeightTemplateId(reqDto.getFeightTemplateId())
                .setProductAttributeCategoryId(reqDto.getProductAttributeCategoryId())
                .setName(reqDto.getName())
                .setPic(reqDto.getPic())
                .setProductSn(reqDto.getProductSn())
                .setDeleteStatus(reqDto.getDeleteStatus())
                .setPublishStatus(reqDto.getPublishStatus())
                .setNewStatus(reqDto.getNewStatus())
                .setRecommandStatus(reqDto.getRecommandStatus())
                .setVerifyStatus(reqDto.getVerifyStatus())
                .setSort(reqDto.getSort())
                .setSale(reqDto.getSale())
                .setPrice(reqDto.getPrice())
                .setPromotionPrice(reqDto.getPromotionPrice())
                .setGiftGrowth(reqDto.getGiftGrowth())
                .setGiftPoint(reqDto.getGiftPoint())
                .setUsePointLimit(reqDto.getUsePointLimit())
                .setSubTitle(reqDto.getSubTitle())
                .setDescription(reqDto.getDescription())
                .setOriginalPrice(reqDto.getOriginalPrice())
                .setStock(reqDto.getStock())
                .setLowStock(reqDto.getLowStock())
                .setUnit(reqDto.getUnit())
                .setWeight(reqDto.getWeight())
                .setPreviewStatus(reqDto.getPreviewStatus())
                .setServiceIds(reqDto.getServiceIds())
                .setKeywords(reqDto.getKeywords())
                .setNote(reqDto.getNote())
                .setAlbumPics(reqDto.getAlbumPics())
                .setDetailTitle(reqDto.getDetailTitle())
                .setDetailDesc(reqDto.getDetailDesc())
                .setDetailHtml(reqDto.getDetailHtml())
                .setDetailMobileHtml(reqDto.getDetailMobileHtml())
                .setPromotionStartTime(reqDto.getPromotionStartTime())
                .setPromotionEndTime(reqDto.getPromotionEndTime())
                .setPromotionPerLimit(reqDto.getPromotionPerLimit())
                .setPromotionType(reqDto.getPromotionType())
                .setBrandName(reqDto.getBrandName())
                .setProductCategoryName(reqDto.getProductCategoryName())
                .setCommission(reqDto.getCommission());
        productService.update(updateProductInput);

        ProductEntity product = productService.getById(reqDto.getId());

        return CommonResult.success(product);
    }

    /**
     * 删除商品信息
     *
     * @param id id
     */
    @ApiOperation("删除商品信息")
    @GetMapping("delete/{id}")
    public CommonResult delete(@PathVariable(value = "id") long id) {
        if (id <= 0) {
            throw new BusinessException("要删除的商品信息id不能为空。");
        }
        productService.delete(id);

        return CommonResult.success(null);
    }

    /**
     * 分页查询商品信息
     *
     * @return 分页后的商品信息
     */
    @ApiOperation("分页查询商品信息")
    @PostMapping("pagedQuery")
    public CommonPage<ProductEntity> pagedQuery(@RequestBody PagedQueryProductReqDto reqDto) {
        PagedQueryProductInput pagedQueryProductInput = new PagedQueryProductInput();
        pagedQueryProductInput
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        Page<ProductEntity> pageInfo = productService.pagedQuery(pagedQueryProductInput);

        return CommonPage.restPage(pageInfo);
    }

    /**
     * 根据id获取商品信息
     *
     * @param id id
     * @return 商品信息
     */
    @ApiOperation("根据id获取商品信息")
    @GetMapping("{id}")
    public CommonResult<ProductEntity> getById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException("要获取的商品信息Id不能为空。");
        }

        ProductEntity product = productService.getById(id);
        if (product == null) {
            throw new BusinessException(String.format("要获取的商品信息id:%s不存在。", id));
        }

        return CommonResult.success(product);
    }

    /**
     * 根据批量id获取商品信息
     *
     * @param ids ids
     * @return 商品信息
     */
    @ApiOperation("根据批量id获取商品信息")
    @PostMapping("listByBatchIds")
    public CommonResult<List<ProductEntity>> listByBatchIds(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("要获取的商品信息Id集合不能为空。");
        }

        List<ProductEntity> product = productService.listByBatchIds(ids);

        return CommonResult.success(product);
    }

}

